package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.ActivityAwardLogService;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.ActivityAwardLogDao;
import com.qdlc.p2p.dal.dto.ActivityAwardLog;
import com.qdlc.p2p.dal.model.ActivityAwardLogModel;
/**
 * 活动奖励记录Service实现
 * @author zlhx
 * @version 2.0
 * @date 2015-06-03 14：23
 */
@Service(value="activityAwardLogService")
public class ActivityAwardLogServiceImpl implements ActivityAwardLogService {
	@Resource
	private ActivityAwardLogDao activityAwardLogDao;
	
	@Override
	public void save(ActivityAwardLog activityAwardLog){
		activityAwardLogDao.save(activityAwardLog);
	}

	@Override
	public PageDataList<ActivityAwardLogModel> manageList(
			ActivityAwardLogModel model) {
		QueryParam param = QueryParam.getInstance();
		if(model!=null){
			if(StringUtil.isNotBlank(model.getSearchName())){
				SearchFilter orFilter1 = new SearchFilter("user.userName", Operators.LIKE, model.getSearchName().trim());
        		SearchFilter orFilter2 = new SearchFilter("beInvitedUser.userName", Operators.LIKE, model.getSearchName().trim());
        		param.addOrFilter(orFilter1, orFilter2);
			}else{
				//名字 检索
				if(!StringUtil.isBlank(model.getUserName())){
					param.addParam("user.userName", Operators.LIKE, model.getUserName());
				}
				if(!StringUtil.isBlank(model.getBeInvitedUserName())){
					param.addParam("beInvitedUser.userName", Operators.LIKE, model.getBeInvitedUserName());
				}
				/*if(StringUtil.isNotBlank(model.getType())){
					param.addParam("type", Operators.LIKE, model.getType());
				}*/
				if(StringUtil.isNotBlank(model.getStartTime())){
					param.addParam("addTime", Operators.GTE, DateUtil.valueOf(model.getStartTime()));
				}
				if(StringUtil.isNotBlank(model.getEndTime())){
					param.addParam("addTime", Operators.LTE, DateUtil.valueOf(model.getEndTime()));
				}
			}
			
		}
		param.addOrder(OrderType.DESC, "id");
		param.addPage(model.getPage(), model.getRows());
		PageDataList<ActivityAwardLog> p = activityAwardLogDao.findAllPageList(param);
		List<ActivityAwardLogModel> list = new ArrayList<ActivityAwardLogModel>();
		PageDataList<ActivityAwardLogModel> plist = new PageDataList<ActivityAwardLogModel>();
		for(ActivityAwardLog activityAwardLog : p.getList()){
			ActivityAwardLogModel activityAwardLogModel = ActivityAwardLogModel.instance(activityAwardLog);
			//为活动方案中 红包、体验券、加息券 赋值中文
			if(activityAwardLog.getUser()!=null){
				activityAwardLogModel.setUserName(activityAwardLog.getUser().getUserName());
			}
			if(activityAwardLog.getBeInvitedUser()!=null){
				activityAwardLogModel.setBeInvitedUserName(activityAwardLog.getBeInvitedUser().getUserName());
			}
			if(activityAwardLog.getUserRedEnvelope()!=null){
				activityAwardLogModel.setRedEnvelopeValue(activityAwardLog.getUserRedEnvelope().getAmount());
			}
			if(activityAwardLog.getExperience()!=null){
				activityAwardLogModel.setExperienceValue(activityAwardLog.getExperience().getAmount());
			}
			if(activityAwardLog.getRateInterest()!=null){
				activityAwardLogModel.setRateInterestVlue(activityAwardLog.getRateInterest().getUpApr());
			}
			list.add(activityAwardLogModel);
		}
		plist.setPage(p.getPage());
		plist.setList(list);
		return plist;
	}
	
}
